// pages/news/news.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    readFlag: true,
    cateName: '通知信息',
    moveStyle: 'left:0',
    currentIdx: 0,
    btnW: wx.getSystemInfoSync().windowWidth / 750 * 120,
    popupFlag: false,
    page: 1,
    limit: 15
  },
  // 获取地址前缀
  getAddress() {
    var IAP = getApp().globalData.host;
    this.setData({
      IAP: IAP
    })
  },
  // 选择消息类型
  choose(e) {
    if (e.currentTarget.dataset.category == this.data.cateName) {
      return
    } else {
      this.setData({
        cateName: e.currentTarget.dataset.category,
        page: 1
      })
      this.getInformationByType()
    }
  },
  // 触摸开始
  touchS: function(e) {
    if (e.touches.length == 1) {
      this.setData({
        moveStyle: 'left:0',
        startX: e.touches[0].clientX,
        currentIdx: e.currentTarget.dataset.idx,
      })
    }
  },
  // 触摸移动
  touchM: function(e) {
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = this.data.startX - moveX
      var moveStyle = '';
      //如果移动距离小于等于0，文本层位置不变
      if (disX <= 0) {
        moveStyle = "left:0";
      } else if (disX > 0 && disX < this.data.btnW) {
        moveStyle = "left:-" + disX + "px";
      } else if (disX > this.data.btnW) {
        moveStyle = "left:-" + this.data.btnW + "px";
      }
      this.setData({
        moveStyle: moveStyle
      })
    }
  },
  // 触摸结束
  touchE(e) {
    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX
      var moveStyle = '';
      //如果移动距离小于等于0，文本层位置不变
      if (disX < this.data.btnW) {
        moveStyle = "left:0";
      } else if (disX >= this.data.btnW) {
        moveStyle = "left:-" + this.data.btnW + "px";
      }
      this.setData({
        moveStyle: moveStyle,
        startX: 0
      })
    }
  },
  // 触摸打断
  touchC(e) {
    this.setData({
      moveStyle: 'left:0',
      startX: 0
    })
  },
  // 删除消息
  deleteEngineering(e) {
    wx.showModal({
      title: '删除提醒',
      content: '是否确认删除本条消息',
      confirmColor: '#ff4c4c',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          wx.request({
            url: this.data.IAP + 'sysnotice/delete',
            header: {
              "token": wx.getStorageSync('token')
            },
            method: "POST",
            data: [e.currentTarget.dataset.idx],
            success: res => {
              if (res.data.code == 0) {
                var info = this.data.information.filter(value => {
                  return value.id != e.currentTarget.dataset.idx
                })
                this.setData({
                  information: info,
                  currentIdx: -1
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 阅读信息
  readnews(e) {
    this.setData({
      popupFlag: true,
      index: e.currentTarget.dataset.idx
    })
    var readArr = wx.getStorageSync('read');
    var isExistence = readArr.indexOf(this.data.information[this.data.index].id);
    if (isExistence == -1) {
      readArr.push(this.data.information[this.data.index].id);
      wx.setStorageSync('read', readArr);
      this.data.information[this.data.index].unread = false
    } else {
      return;
    }
  },
  // 关闭弹窗
  closePopup() {
    this.setData({
      popupFlag: false,
      information: this.data.information
    })

  },
  // 根据消息类型获取消息
  getInformationByType() {
    wx.request({
      url: this.data.IAP + 'sysnotice/list',
      header: {
        "token": wx.getStorageSync('token')
      },
      data: {
        page: this.data.page,
        limit: this.data.limit,
        type: this.data.cateName
      },
      success: res => {
        if (res.data.code == 0) {
          if (this.data.page == 1) {
            this.setData({
              information: res.data.page.list,
              page: this.data.page + 1
            })
          } else {
            if (res.data.page.list.length) {
              this.setData({
                information: this.data.information.concat(res.data.page.list),
                page: this.data.page + 1
              })
            } else {
              wx.showToast({
                title: '没有更多数据了',
                icon: 'none'
              })
            }
          }
          var readArr = wx.getStorageSync('read')
          this.data.information.forEach((value, index) => {
            var isExistence = readArr.indexOf(value.id);
            if (isExistence == -1) {
              value.unread = true
            } else {
              value.unread = false
            }
          })
          this.setData({
            information: this.data.information
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getInformationByType();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})